package com.wgj.android.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.FragmentManager;
import android.view.WindowManager;

import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.accesslibility.config.WXConfginUtil;
import com.wgj.android.accesslibility.service.WGJAccessServier;
import com.wgj.android.dialog.WindowPermissionTipsDialog;
import com.wgj.android.permission.WindowManagerPermission;
import com.wgj.android.ui.activity.WXHongBaoActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 */

public class WXAccessUtil {

    /**
     * 检测微信综合助手运行环镜
     * @param context
     * @return
     */
    public  static  boolean  checkAccessRunAmbient(Context context){
        PackageInfo packageInfo= null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo("com.tencent.mm", 0);
            if(packageInfo.versionCode<1200){
                XToast.error("当前微信版本过低请升级微信到6.6.2");
                return false;
            }
            if(packageInfo.versionCode>1420){
                XToast.error("当前微信版本过高,请使用微信6.6.2");
                return false;
            }
            return checkAccessIsRuning(context);
        } catch (Exception  e) {
            e.printStackTrace();
        }
        return false;

    }

    private static   boolean  checkAccessIsRuning(final Context context){
        if(!WGJAccessServier.isRun()){
            new XAlertMsgDialog(context,"检测到旺旺通综合助手未打开,是否前往打开?").setPositiveButton("开启").setListener(new XAlertMsgDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean b) {
                    if(b){
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        ((Activity)context).startActivity(intent);
                    }
                }
            }).show();
            return false;
        }
        return  true;

    }
    public  static  void checkWindowManagerPermission(Context context,FragmentManager manager , WindowPermissionTipsDialog.OnNextCallback callback){
        if(Build.VERSION.SDK_INT< Build.VERSION_CODES.M){//如果当前版本低于6.0则提示
            if(WXConfginUtil.isNotTipsWindowPermission(context)){
                callback.onNext();
                }else {
                    new WindowPermissionTipsDialog().setCallback(callback).show(manager,"WindowPermissionTipsDialog");
             }
         }else {
            if(WindowManagerPermission.checkPermission(context)){
                       callback.onNext();
            }else {
                WindowManagerPermission.applyPermission(context);
            }
        }


    }




}
